package com.person.android.ui.activity;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mobstat.SendStrategyEnum;
import com.baidu.mobstat.StatService;
import com.person.android.dao.UIRenderParameter;
import com.person.android.formatts.R;
import com.person.android.resource.XMLReader;
import com.person.android.ui.fragments.HomeFragment;
import com.person.android.ui.fragments.PersonFragment;
import com.person.android.ui.fragments.SettingFragment;
import com.person.android.ui.fragments.TransformFragment;
import com.person.android.utils.LogUtils;


public class MainActivity extends Activity implements View.OnClickListener {

    private TextView mNavTxt;                           //导航界面显示
    private ImageView mHomeImv;                         //主页按钮
    private ImageView mPersonImv;                       //个人按钮
    private ImageView mSettingImv;                      //设置按钮
    private ImageView mTransformImv;                    //转换按钮

    private HomeFragment mHomeFragment;                 //主页界面
    private PersonFragment mPersonFragment;             //个人中心界面
    private SettingFragment mSettingFragment;           //设置界面
    private TransformFragment mTransformFragment;       //转换界面

    private FragmentManager mFragmentManager;           //fragment管理器
    private RelativeLayout mBottomContainer;            //底部容器
    private RelativeLayout mHeaderContainer;            //顶部说明栏

    private long exitTime = 0;                          //退出判断时间记录器

    private enum CUR_UI {HOME, TRANSFORM, SETTING, PERSON}

    private CUR_UI mCurrentUIItem = CUR_UI.TRANSFORM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        openBaiDuStaticsService();

        findViews();
        setListeners();
        renderUIColor();
        setImvNormalResource();
        gotoSpecifyView(mTransformFragment);
        setSpecifyImvPressResource(mTransformImv, R.drawable.transform_press, R.string.fragment_transform);
        LogUtils.logInfo(LogUtils.FileListActivityFlag, "MainActivity onCreate...");
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtils.logInfo(LogUtils.FileListActivityFlag, "MainActivity onStart...");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        LogUtils.logInfo(LogUtils.FileListActivityFlag, "MainActivity onRestart...");
    }

    //开启百度统计任务
    private void openBaiDuStaticsService(){
        StatService.setSessionTimeOut(30);
        StatService.setOn(this, StatService.EXCEPTION_LOG);
        StatService.setLogSenderDelayed(0);
        StatService.setSendLogStrategy(this, SendStrategyEnum.SET_TIME_INTERVAL, 1, false);
        StatService.setDebugOn(true);
    }

    private void findViews() {
        mHomeImv = (ImageView) findViewById(R.id.homeImv);
        mNavTxt = (TextView) findViewById(R.id.header_txt);
        mPersonImv = (ImageView) findViewById(R.id.personImv);
        mSettingImv = (ImageView) findViewById(R.id.settingImv);
        mTransformImv = (ImageView) findViewById(R.id.transformImv);
        mHeaderContainer = (RelativeLayout) findViewById(R.id.header_bar);
        mBottomContainer = (RelativeLayout) findViewById(R.id.bottomContainer);
        mHomeFragment = new HomeFragment();
        mPersonFragment = new PersonFragment();
        mSettingFragment = new SettingFragment();
        mTransformFragment = new TransformFragment();

        mFragmentManager = getFragmentManager();

    }

    private void renderUIColor() {
        UIRenderParameter parameter = XMLReader.getUIParameter(this);
        int mUIColorIndexValue = parameter.mUIColorValue;
        int mNavColorIndexValue = parameter.mNavColorValue;
        switch (mUIColorIndexValue) {
            case 0:
                findViewById(R.id.container).setBackgroundResource(R.color.beige);
                break;
            case 1:
                findViewById(R.id.container).setBackgroundResource(R.color.darkslategray);
                break;
            case 2:
                findViewById(R.id.container).setBackgroundResource(R.color.skyblue);
                break;
            case 3:
                findViewById(R.id.container).setBackgroundResource(R.color.darkgray);
                break;
            case 4:
                findViewById(R.id.container).setBackgroundResource(R.color.palegreen);
                break;
            case 5:
                findViewById(R.id.container).setBackgroundResource(R.color.darkorchid);
                break;
            case 6:
                findViewById(R.id.container).setBackgroundResource(R.color.aquamarine);
                break;
        }
        switch (mNavColorIndexValue) {
            case 0:
                mBottomContainer.setBackgroundResource(R.color.beige);
                mHeaderContainer.setBackgroundResource(R.color.beige);
                break;
            case 1:
                mBottomContainer.setBackgroundResource(R.color.darkslategray);
                mHeaderContainer.setBackgroundResource(R.color.darkslategray);
                break;
            case 2:
                mBottomContainer.setBackgroundResource(R.color.skyblue);
                mHeaderContainer.setBackgroundResource(R.color.skyblue);
                break;
            case 3:
                mBottomContainer.setBackgroundResource(R.color.darkgray);
                mHeaderContainer.setBackgroundResource(R.color.darkgray);
                break;
            case 4:
                mBottomContainer.setBackgroundResource(R.color.palegreen);
                mHeaderContainer.setBackgroundResource(R.color.palegreen);
                break;
            case 5:
                mBottomContainer.setBackgroundResource(R.color.darkorchid);
                mHeaderContainer.setBackgroundResource(R.color.darkorchid);
                break;
            case 6:
                mBottomContainer.setBackgroundResource(R.color.aquamarine);
                mHeaderContainer.setBackgroundResource(R.color.aquamarine);
                break;
        }
    }

    private void setListeners() {
        mHomeImv.setOnClickListener(this);
        mPersonImv.setOnClickListener(this);
        mSettingImv.setOnClickListener(this);
        mTransformImv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        setImvNormalResource();
        switch (v.getId()) {
            case R.id.homeImv:
                mCurrentUIItem = CUR_UI.HOME;
                setSpecifyImvPressResource(mHomeImv, R.drawable.home_press, R.string.fragment_home);
                gotoSpecifyView(mHomeFragment);
                break;
            case R.id.personImv:
                mCurrentUIItem = CUR_UI.PERSON;
                setSpecifyImvPressResource(mPersonImv, R.drawable.person_press, R.string.fragment_person);
                gotoSpecifyView(mPersonFragment);
                break;
            case R.id.settingImv:
                mCurrentUIItem = CUR_UI.SETTING;
                setSpecifyImvPressResource(mSettingImv, R.drawable.setting_press, R.string.fragment_setting);
                gotoSpecifyView(mSettingFragment);
                break;
            case R.id.transformImv:
                mCurrentUIItem = CUR_UI.TRANSFORM;
                setSpecifyImvPressResource(mTransformImv, R.drawable.transform_press, R.string.fragment_transform);
                gotoSpecifyView(mTransformFragment);
                break;
        }
    }

    private void gotoSpecifyView(Fragment view) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.replace(R.id.container, view);
        transaction.commit();
    }

    //设置不同的标记颜色
    private void setSpecifyImvPressResource(ImageView view, int resId, int txtId) {
        mNavTxt.setText(txtId);
        view.setBackgroundResource(resId);
    }

    private void setImvNormalResource() {
        mHomeImv.setBackgroundResource(R.drawable.home_normal);
        mPersonImv.setBackgroundResource(R.drawable.person_normal);
        mSettingImv.setBackgroundResource(R.drawable.setting_normal);
        mTransformImv.setBackgroundResource(R.drawable.transform_normal);
    }

    //设置导航栏的颜色
    public void setNavBackgroundResource(int colorId) {
        mHeaderContainer.setBackgroundResource(colorId);
        mBottomContainer.setBackgroundResource(colorId);
    }

    //设置UI的颜色
    public void setUIBackgroundResource(int colorId) {
        findViewById(R.id.container).setBackgroundResource(colorId);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (mCurrentUIItem != CUR_UI.TRANSFORM) {
                setImvNormalResource();
                setSpecifyImvPressResource(mTransformImv, R.drawable.transform_press, R.string.fragment_transform);
                mCurrentUIItem = CUR_UI.TRANSFORM;
                gotoSpecifyView(mTransformFragment);
                return true;
            }
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(this, R.string.system_exit, Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
